#include "ConfigParser.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <filesystem>

namespace fs = std::filesystem;

bool ConfigParser::load(const std::string& configPath) {
    configMap.clear(); // 清空现有配置
    
    try {
        fs::path absPath = fs::absolute(configPath);
        std::cout << "正在加载配置文件: " << absPath << std::endl;
        
        if (!fs::exists(absPath)) {
            std::cerr << "错误：配置文件不存在: " << absPath << std::endl;
            return false;
        }

        std::ifstream file(absPath);
        std::string line;
        
        while (std::getline(file, line)) {
            // 跳过空行和注释行
            if (line.empty() || line[0] == '#') continue;
            
            size_t delimiterPos = line.find('=');
            if (delimiterPos != std::string::npos) {
                std::string key = line.substr(0, delimiterPos);
                std::string value = line.substr(delimiterPos + 1);
                
                // 去除首尾空格
                key.erase(0, key.find_first_not_of(" \t"));
                key.erase(key.find_last_not_of(" \t") + 1);
                value.erase(0, value.find_first_not_of(" \t"));
                value.erase(value.find_last_not_of(" \t") + 1);
                
                // 处理可能存在的引号
                if (!value.empty()) {
                    if (value.front() == '"' && value.back() == '"') {
                        value = value.substr(1, value.length() - 2);
                    }
                    else if (value.front() == '\'' && value.back() == '\'') {
                        value = value.substr(1, value.length() - 2);
                    }
                }
                
                configMap[key] = value;
            }
        }
        
        // 检查必要配置项
        if (configMap.empty()) {
            std::cerr << "错误：配置文件为空或格式不正确" << std::endl;
            return false;
        }
        
        return true;
    } catch (const std::exception& e) {
        std::cerr << "配置文件加载错误: " << e.what() << std::endl;
        return false;
    }
}

std::string ConfigParser::get(const std::string& key, const std::string& defaultValue) const {
    auto it = configMap.find(key);
    return it != configMap.end() ? it->second : defaultValue;
}

void ConfigParser::printAll() const {
    if (configMap.empty()) {
        std::cout << "无有效配置项" << std::endl;
        return;
    }
    
    std::cout << "=== 所有配置项 ===" << std::endl;
    for (const auto& [key, value] : configMap) {
        std::cout << key << " = " << value << std::endl;
    }
}

bool ConfigParser::validatePaths() const {
    if (configMap.empty()) return false;
    
    std::cout << "\n=== 路径验证 ===" << std::endl;
    bool allValid = true;
    
    for (const auto& key : pathKeys) {
        auto it = configMap.find(key);
        if (it != configMap.end() && !it->second.empty()) {
            std::cout << "检查 " << key << ": " << it->second << " ... ";
            if (fs::exists(it->second)) {
                std::cout << "存在" << std::endl;
            } else {
                std::cout << "不存在" << std::endl;
                allValid = false;
            }
        }
    }
    
    return allValid;
}